package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.adapter.Fill_Information2_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Information;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class GeRen_Update_Msg_Gxtd_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private String access_token, face_tixing, role_juese, status_name, aim_name;
    private int type;
    private Map<Integer, String> list_tixing, list_style, list_aim, list_work;
    private Map<String, String> list_tixing_name, list_style_name, list_aim_name, list_work_name;
    private Set<String> list_i_face, list_i_face_name, list_i_style, list_i_style_name, list_i_aim, list_i_aim_name, list_i_work, list_i_work_name;
    private MyGridView gv_fill_item_type, gv_fill_item_juese;
    private TextView tv_feedback_tijiao, tv_chat_nickname, tv_fill_info2_type, tv_fill_info2_juese;
    private User_Information user_information;
    private Fill_Information2_Adapter fill_information2_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_update_msg_gxtd);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        type = intent.getIntExtra("type", 0);
        user_information = (User_Information) intent.getSerializableExtra("user_information");
        back = findViewById(R.id.back);
        gv_fill_item_type = findViewById(R.id.gv_fill_item_type);
        gv_fill_item_juese = findViewById(R.id.gv_fill_item_juese);
        tv_chat_nickname = findViewById(R.id.tv_chat_nickname);
        tv_feedback_tijiao = findViewById(R.id.tv_feedback_tijiao);
        tv_fill_info2_type = findViewById(R.id.tv_fill_info2_type);
        tv_fill_info2_juese = findViewById(R.id.tv_fill_info2_juese);
        back.setOnClickListener(this);
        tv_feedback_tijiao.setOnClickListener(this);
        gv_fill_item_type.setVerticalSpacing(15);
        gv_fill_item_type.setHorizontalSpacing(15);
        gv_fill_item_juese.setVerticalSpacing(15);
        gv_fill_item_juese.setHorizontalSpacing(15);
        SharedPreferences preferences = getSharedPreferences("update_message", Context.MODE_PRIVATE);
        Set<String> aim = preferences.getStringSet("aim", null);
        Set<String> style = preferences.getStringSet("style", null);
        Set<String> i_face = preferences.getStringSet("i_face", null);
        Set<String> i_style = preferences.getStringSet("i_style", null);
        Set<String> aim_set_name1 = preferences.getStringSet("aim_name", null);
        String status_name1 = preferences.getString("status_name", "");
        String face_tixing1 = preferences.getString("face_tixing", "");
        String role_juese1 = preferences.getString("role_juese", "");
        Set<String> style_name1 = preferences.getStringSet("style_name", null);
        Set<String> i_face_name1 = preferences.getStringSet("i_face_name", null);
        Set<String> i_style_name1 = preferences.getStringSet("i_style_name", null);
        if (type == 0) {
            gv_fill_item_juese.setVisibility(View.VISIBLE);
            tv_fill_info2_type.setVisibility(View.VISIBLE);
            tv_fill_info2_juese.setVisibility(View.VISIBLE);
            tv_chat_nickname.setText("基本信息");
            tv_fill_info2_type.setText("角色");
            tv_fill_info2_juese.setText("体型");
            Fill_Information2_Adapter adapter = new Fill_Information2_Adapter(this, user_information, 3);
            adapter.setName(face_tixing1);
            face_tixing = face_tixing1;
            gv_fill_item_juese.setAdapter(adapter);
            gv_fill_item_juese.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    adapter.setName("");
                    // 选中的值
                    Integer int_i_face = user_information.getFace().get(position).getValue();
                    face_tixing = user_information.getFace().get(position).getName();
                    adapter.setSelect(position);
                    adapter.notifyDataSetChanged();
                }
            });

            fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 2);
            fill_information2_adapter.setName(role_juese1);
            role_juese = role_juese1;
            gv_fill_item_type.setAdapter(fill_information2_adapter);
            gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    fill_information2_adapter.setName("");
                    // 选中的值
                    Integer int_i_face = user_information.getRole().get(position).getValue();
                    role_juese = user_information.getRole().get(position).getName();
                    fill_information2_adapter.setSelect(position);
                    fill_information2_adapter.notifyDataSetChanged();
                }
            });
        } else if (type == 1) {
            gv_fill_item_juese.setVisibility(View.GONE);
            tv_fill_info2_type.setVisibility(View.GONE);
            tv_fill_info2_juese.setVisibility(View.GONE);
            list_work = new HashMap<>();
            list_work_name = new HashMap<>();
            list_i_work = new HashSet<>();
            list_i_work_name = new HashSet<>();
            tv_chat_nickname.setText("个性特点");
            if (style_name1 != null) {
                for (String str : style){
                    list_work.put(Integer.parseInt(str), "1");
                }
                for (String str : style_name1){
                    list_work_name.put(str, "1");
                }
                for (int i = 0; i < user_information.getStyle().size(); i++) {
                    String value_name = list_work_name.get(user_information.getStyle().get(i).getName());
                    if (ToolUtils.isEmpty(value_name)) { // 没选中
                        user_information.getStyle().get(i).setChecked(false);
                    } else { // 已选中
                        user_information.getStyle().get(i).setChecked(true);
                    }
                }
            }
            fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 4);
            gv_fill_item_type.setAdapter(fill_information2_adapter);
            gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer int_i_face = user_information.getStyle().get(position).getValue();
                    String name = user_information.getStyle().get(position).getName();
                    String value = list_work.get(int_i_face);
                    String value_name = list_work_name.get(name);
                    if (ToolUtils.isEmpty(value)) { // 没选中
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_work.put(int_i_face, "1");
                    } else {
                        list_work.remove(int_i_face);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
                    }
                    if (ToolUtils.isEmpty(value_name)) {
                        list_work_name.put(name, "1");
                    } else {
                        list_work_name.remove(name);
                    }
                }
            });
        } else if (type == 2) {
            gv_fill_item_juese.setVisibility(View.VISIBLE);
            tv_fill_info2_type.setVisibility(View.VISIBLE);
            tv_fill_info2_juese.setVisibility(View.VISIBLE);
            list_aim = new HashMap<>();
            list_aim_name = new HashMap<>();
            list_i_aim = new HashSet<>();
            list_i_aim_name = new HashSet<>();
            tv_chat_nickname.setText("个人信息");
            tv_fill_info2_type.setText("交友的目的");
            tv_fill_info2_juese.setText("感情状况");
            Fill_Information2_Adapter adapter = new Fill_Information2_Adapter(this, user_information, 8);
            adapter.setName(status_name1);
            status_name = status_name1;
            gv_fill_item_juese.setAdapter(adapter);
            gv_fill_item_juese.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    adapter.setName("");
                    // 选中的值
                    Integer int_i_face = user_information.getStatus().get(position).getValue();
                    status_name = user_information.getStatus().get(position).getName();
                    adapter.setSelect(position);
                    adapter.notifyDataSetChanged();
                }
            });

            if (aim_set_name1 != null) {
                for (String str : aim){
                    list_aim.put(Integer.parseInt(str), "1");
                }
                for (String str : aim_set_name1){
                    list_aim_name.put(str, "1");
                }
                for (int i = 0; i < user_information.getAim().size(); i++) {
                    String value_name = list_aim_name.get(user_information.getAim().get(i).getName());
                    if (ToolUtils.isEmpty(value_name)) { // 没选中
                        user_information.getAim().get(i).setChecked(false);
                    } else { // 已选中
                        user_information.getAim().get(i).setChecked(true);
                    }
                }
            }
            fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 6);
            gv_fill_item_type.setAdapter(fill_information2_adapter);
            gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer int_i_face = user_information.getAim().get(position).getValue();
                    aim_name = user_information.getAim().get(position).getName();
                    String value = list_aim.get(int_i_face);
                    String value_name = list_aim_name.get(aim_name);
                    if (ToolUtils.isEmpty(value)) { // 没选中
                        list_aim.put(int_i_face, "1");
                    } else {
                        list_aim.remove(int_i_face);
                    }
                    if (ToolUtils.isEmpty(value_name)) {
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_aim_name.put(aim_name, "1");
                    } else {
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
                        list_aim_name.remove(aim_name);
                    }
                }
            });
        } else if (type == 3) {
            gv_fill_item_juese.setVisibility(View.VISIBLE);
            tv_fill_info2_type.setVisibility(View.VISIBLE);
            tv_fill_info2_juese.setVisibility(View.VISIBLE);
            list_style = new HashMap<>();
            list_style_name = new HashMap<>();
            list_i_style = new HashSet<>();
            list_i_style_name = new HashSet<>();

            list_tixing = new HashMap<>();
            list_tixing_name = new HashMap<>();
            list_i_face = new HashSet<>();
            list_i_face_name = new HashSet<>();
            tv_chat_nickname.setText("喜欢的类型");
            tv_fill_info2_type.setText("体型");
            tv_fill_info2_juese.setText("个性特点");
            if (i_face_name1 != null) {
                for (String str : i_face){
                    list_tixing.put(Integer.parseInt(str), "1");
                }
                for (String str : i_face_name1){
                    list_tixing_name.put(str, "1");
                }
                for (int i = 0; i < user_information.getFace().size(); i++) {
                    String value_name = list_tixing_name.get(user_information.getFace().get(i).getName());
                    if (ToolUtils.isEmpty(value_name)) { // 没选中
                        user_information.getFace().get(i).setChecked(false);
                    } else { // 已选中
                        user_information.getFace().get(i).setChecked(true);
                    }
                }
            }
            Fill_Information2_Adapter adapter = new Fill_Information2_Adapter(this, user_information, 9);
            gv_fill_item_juese.setAdapter(adapter);
            gv_fill_item_juese.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer int_i_face = user_information.getFace().get(position).getValue();
                    String name = user_information.getFace().get(position).getName();
                    String value = list_tixing.get(int_i_face);
                    String value_name = list_tixing_name.get(name);
                    if (ToolUtils.isEmpty(value)) { // 没选中
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_tixing.put(int_i_face, "1");
                    } else {
                        list_tixing.remove(int_i_face);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
                    }
                    if (ToolUtils.isEmpty(value_name)) {
                        list_tixing_name.put(name, "1");
                    } else {
                        list_tixing_name.remove(name);
                    }
                }
            });

            if (i_style_name1 != null) {
                for (String str : i_style){
                    list_style.put(Integer.parseInt(str), "1");
                }
                for (String str : i_style_name1){
                    list_style_name.put(str, "1");
                }
                for (int i = 0; i < user_information.getStyle().size(); i++) {
                    String value_name = list_style_name.get(user_information.getStyle().get(i).getName());
                    if (ToolUtils.isEmpty(value_name)) { // 没选中
                        user_information.getStyle().get(i).setChecked(false);
                    } else { // 已选中
                        user_information.getStyle().get(i).setChecked(true);
                    }
                }
            }
            fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 4);
            gv_fill_item_type.setAdapter(fill_information2_adapter);
            gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer key = user_information.getStyle().get(position).getValue();
                    String name = user_information.getStyle().get(position).getName();
                    String value = list_style.get(key);
                    String style_name = list_style_name.get(name);
                    if (ToolUtils.isEmpty(value)) { // 没选中
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_style.put(key, "1");
                    } else {
                        list_style.remove(key);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
                    }
                    if (ToolUtils.isEmpty(style_name)) {
                        list_style_name.put(name, "1");
                    } else {
                        list_style_name.remove(name);
                    }
                }
            });
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_feedback_tijiao:
                SharedPreferences preferences = getSharedPreferences("update_message", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                if (type == 0) {
                    editor.putString("face_tixing", face_tixing);
                    editor.putString("role_juese", role_juese);
                } else if (type == 1) {
                    if (list_work != null) {
                        for (Map.Entry<Integer, String> entry : list_work.entrySet()) {
                            list_i_work.add(entry.getKey() + "");
                        }
                        for (Map.Entry<String, String> entry : list_work_name.entrySet()) {
                            list_i_work_name.add(entry.getKey() + "");
                        }
                    }
                    editor.putStringSet("style", list_i_work);
                    editor.putStringSet("style_name", list_i_work_name);
                } else if (type == 2) {
                    if (list_aim != null) {
                        for (Map.Entry<Integer, String> entry : list_aim.entrySet()) {
                            list_i_aim.add(entry.getKey() + "");
                        }
                        for (Map.Entry<String, String> entry : list_aim_name.entrySet()) {
                            list_i_aim_name.add(entry.getKey() + "");
                        }
                    }
                    editor.putString("status_name", status_name);
                    editor.putStringSet("aim", list_i_aim);
                    editor.putStringSet("aim_name", list_i_aim_name);
                } else {
                    if (list_tixing != null) {
                        for (Map.Entry<Integer, String> entry : list_tixing.entrySet()) {
                            list_i_face.add(entry.getKey() + "");
                        }
                        for (Map.Entry<String, String> entry : list_tixing_name.entrySet()) {
                            list_i_face_name.add(entry.getKey() + "");
                        }
                    }
                    editor.putStringSet("i_face", list_i_face);
                    editor.putStringSet("i_face_name", list_i_face_name);

                    if (list_style != null) {
                        for (Map.Entry<Integer, String> entry : list_style.entrySet()) {
                            list_i_style.add(entry.getKey() + "");
                        }
                        for (Map.Entry<String, String> entry : list_style_name.entrySet()) {
                            list_i_style_name.add(entry.getKey() + "");
                        }
                    }
                    editor.putStringSet("i_style", list_i_style);
                    editor.putStringSet("i_style_name", list_i_style_name);
                }
                editor.apply();
                finish();
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_xyf_Url + access_token + "&limit=10", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
//                        lv_xyf_jilu.setAdapter();
                    } else {
                        ToolUtils.showToast(GeRen_Update_Msg_Gxtd_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(GeRen_Update_Msg_Gxtd_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
